package com.zhongjian.technologycal.mall.activity.main.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.AccordionTransformer;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.main.fragment.HotRecommendFragment;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.BusinessCollegaBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.PicassoImageLoader;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/11/14.
 * 商学院页面实现
 */

public class BusinessCollegeActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.main_tab)
    TabLayout mTabLayout;
    @BindView(R.id.main_vp)
    ViewPager mViewPager;
    @BindView(R.id.business_banner)
    Banner business_banner;
    private List<String> arrayList = new ArrayList<>();
    //当标签数目小于等于4个时，标签栏不可滑动
    public static final int MOVABLE_COUNT = 4;
    private ArrayList<String> tabs;
    private ArrayList<String> tabsId;
    private List<BusinessCollegaBean> listItems = new ArrayList<>();
    @Override
    protected void beforeInit() {
        super.beforeInit();
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("商学院");
        initViewRequestCollegeHome();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_business_college;
    }

    private void initViewRequestCollegeHome() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext()).getUrlRequestCommercialcollegeHomePage(new Subscriber<ResponseBody>() {
            @Override
            public void onStart() {
                super.onStart();
                LoadingDialog.showDialogForLoading(BusinessCollegeActivity.this);
            }

            @Override
            public void onCompleted() {
                LoadingDialog.cancelDialogForLoading();

            }

            @Override
            public void onError(Throwable e) {
                LoadingDialog.cancelDialogForLoading();
                ToastUitl.showShort(e.getMessage());

            }

            @Override
            public void onNext(ResponseBody responseBody) {
                LoadingDialog.cancelDialogForLoading();
                try {
                    Gson gson = new Gson();
                    BusinessCollegaBean businessCollegaBean = gson.fromJson(responseBody.string(), BusinessCollegaBean.class);
                    tabs = new ArrayList<>();
                    tabsId = new ArrayList<>();
                    for (int i = 0; i < businessCollegaBean.getData().getCommercialCollegeCourse().size(); i++) {
                        listItems.add(businessCollegaBean);
                        tabs.add(listItems.get(i).getData().getCommercialCollegeCourse().get(i).getTypename());
                        tabsId.add(String.valueOf(listItems.get(i).getData().getCommercialCollegeCourse().get(i).getId()));
                    }
                    arrayList.add(BaseContacts.BASE_URL + listItems.get(0).getData().getCommercialCollegePic());
                    //MODE_FIXED标签栏不可滑动，各个标签会平分屏幕的宽度
                    mTabLayout.setTabMode(tabs.size() <= MOVABLE_COUNT ? TabLayout.MODE_FIXED : TabLayout.MODE_SCROLLABLE);
                    mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), tabs));
                    mTabLayout.setupWithViewPager(mViewPager);
                    business_banner.setImages(arrayList)
                            .setImageLoader(new PicassoImageLoader())
                            .setOnBannerListener(new OnBannerListener() {
                                @Override
                                public void OnBannerClick(int position) {


                                }

                            })
                            .start();
                    business_banner.setBannerAnimation(AccordionTransformer.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<String> titles;

        public MyPagerAdapter(FragmentManager fm, ArrayList<String> list) {
            super(fm);
            this.titles = list;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public Fragment getItem(int position) {
            Bundle b = new Bundle();
            b.putString("title", titles.get(position));
            b.putString("tabsId", tabsId.get(position));
            return HotRecommendFragment.getInstance(b);
        }
    }

}
